package adventure.commands.admin;

import adventure.*;
import adventure.commands.*;
import adventure.placeable.Item;
import adventure.placeable.PlaceableManager;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class SpawnItem extends AbstractCommand {

    PlaceableManager placeables;

    public SpawnItem(Adventure a) {
        super(a, new String[]{"SPAWNITEM"});
        placeables = (PlaceableManager) ((AdventureEdit) a).getPlaceableManager();
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (p.isAdmin()) {
            if (args.length == 1) {
                return listItems(p);
            } else if (args.length == 2) {
                if (isInteger(args[1])) {
                    return spawnItem(p, 1, Integer.parseInt(args[1]));
                }
                return "Spawn which item?\nSyntax: spawnitem [[<quantity>] <ID>]";
            } else if (args.length == 3) {
                if (isInteger(args[1])) {
                    if (isInteger(args[2])) {
                        return spawnItem(p, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
                    }
                    return "Spawn which item?\nSyntax: spawnitem [[<quantity>] <ID>]";
                }

                return "Spawn which item?\nSyntax: spawnitem [[<quantity>] <ID>]";
            } else {
                return "Spawn which item?\nSyntax: spawnitem [[<quantity>] <ID>]";
            }
        } else {
            return "Command not recognised.";
        }
    }

    private String listItems(Player p) {

        int c = 1;
        Item item;
        String divider = ColourUtil.colourise(
                " +----------------------------------------------------------------------------+",
                ColourUtil.darkCyan);

        String tableOut = divider
                + "\n"
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("ID  ", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Name               ", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Type   ", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Modifier", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Bound", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Temp/tks", ColourUtil.cyan)
                + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                + ColourUtil.colourise("Value", ColourUtil.cyan)
                + ColourUtil.colourise(" |", ColourUtil.darkCyan)
                + "\n"
                + divider;

        while ((item = placeables.getItem(c)).getID() != 0) {
            tableOut +=
                    "\n"
                    + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                    + ColourUtil.colourise(addWhiteSpace(item.getID() + "", 4), ColourUtil.cyan)
                    + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                    + ColourUtil.colourise(addWhiteSpace(item.getName(), 19), ColourUtil.cyan)
                    + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                    + ColourUtil.colourise(addWhiteSpace(item.getItemType(), 7), ColourUtil.cyan)
                    + ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                    + ColourUtil.colourise(addWhiteSpace(item.getModifier() + "", 8), ColourUtil.cyan)
                    + ColourUtil.colourise(" | ", ColourUtil.darkCyan);
            if (item.isBound()) {
                tableOut += ColourUtil.colourise(addWhiteSpace("Yes", 5), ColourUtil.cyan);
            } else {
                tableOut += ColourUtil.colourise(addWhiteSpace("No", 5), ColourUtil.cyan);
            }
            tableOut += ColourUtil.colourise(" | ", ColourUtil.darkCyan);

            if (item.isTemporary()) {
                tableOut += ColourUtil.colourise(addWhiteSpace(item.getTicksRemaining() + "", 8), ColourUtil.cyan);
            } else {
                tableOut += ColourUtil.colourise(addWhiteSpace("No", 8), ColourUtil.cyan);
            }
            tableOut += ColourUtil.colourise(" | ", ColourUtil.darkCyan)
                    + ColourUtil.colourise(addWhiteSpace(item.getCurrencyValue() + "", 5), ColourUtil.cyan)
                    + ColourUtil.colourise(" |", ColourUtil.darkCyan);

            c++;
        }

        tableOut += "\n" + divider;

        return tableOut;
    }

    private String addWhiteSpace(String input, int maxSize) {
        if (input.length() > maxSize) {
            input = input.substring(0, maxSize - 2) + "..";
        } else {
            while (input.length() < maxSize) {
                input += " ";
            }

        }
        return ColourUtil.colourise(input, ColourUtil.cyan);
    }

    private String spawnItem(Player p, int quant, int id) {
        if (quant > 0) {
            Item item = placeables.getItem(id);
            int x = p.x();
            int y = p.y();
            int z = p.z();

            if (item.getID() > 0) {
                String quantOut;
                if (quant > 1) {
                    quantOut = quant + " ";
                } else {
                    quantOut = "a ";
                }

                for (int c = 0; c < quant; c++) {
                    rooms.addItemToRoom(item.cloneThis(), x, y, z);
                }

                out.broadcastLocalToPlayer(p,
                        ColourUtil.colourise(p.getAliveName(), ColourUtil.player)
                        + " has spawned "
                        + quantOut
                        + ColourUtil.colourise(item.getName(), ColourUtil.item)
                        + ".");
                
                return "You have spawned "
                        + quantOut
                        + ColourUtil.colourise(item.getName(), ColourUtil.item)
                        + ".";
                
            }
            return "That is not a valid item ID! Type 'spawnitem' to view a list of all items.";
        }
        return "You must specify a quantity of 1 or more, or leave quantity blank!\nSyntax: spawnitem [[<quantity>] <ID>]";
    }

    private boolean isInteger(String arg) {
        try {
            Integer.parseInt(arg);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
